# -*- coding:utf-8 -*-

from django.db import models
from django.forms import ModelForm
import ltyx.settings
from django.contrib.auth.models import User

'''用户分组表'''
class Banji(models.Model):
    period=models.CharField('届',max_length=10,)
    ban=models.CharField('班',max_length=10,)
    subject=models.CharField('学科（非教师不用填写）',max_length=10,null =True, blank=True,)

'''用户分组表'''
class EmployeeGroup(models.Model):
    employee_type=models.CharField('用户组',max_length=10,)
    
    class Meta:
        verbose_name = '用户分组'
        verbose_name_plural = '用户分组'
        db_table = 'EmployeeGroup' 
        #app_label = u"用户管理"  
        
    def __unicode__(self):
        return  self.employee_type

'''
用户表
nickname 昵称
employee_group 用户所在组
period 属于那届
届=当前年份-年级
'''
BAN_CHOICES = (
('1', '一班'),
('2', '二班'),
('3', '三班'),
('4', '四班'),
('5', '五班'),
('6', '六班'),
('7', '七班'),
('8', '八班'),
)
class Employee(models.Model):
    #自定义性别
    SEX_CHOICES = (
    ('N', '男'),
    ('V', '女'),
    )
    user = models.OneToOneField(User,verbose_name='帐号')
    nickname = models.CharField('昵称',max_length=20)
    sex=models.CharField('性别',max_length=10,null =True, blank=True,choices=SEX_CHOICES)
    birthday=models.CharField('生日',max_length=10,null =True, blank=True,)
    address=models.CharField('地址',max_length=25,null =True, blank=True,)    
    employee_group =  models.ForeignKey(EmployeeGroup,verbose_name='用户分组')
    phone = models.CharField('电话',max_length=20,null =True, blank=True,)
    job = models.CharField('职位',max_length=10,null =True, blank=True,)
    # period = models.CharField('届',max_length=20,)
    # banji = models.CharField('班级',max_length=20,null =True, blank=True,choices=BAN_CHOICES)


    class Meta:
        verbose_name = '用户管理'
        verbose_name_plural = '用户管理'
        db_table = 'Employee' 
        #app_label = u"用户管理"  
        
    def __unicode__(self):
        return  self.nickname

    
    # def getclass(self):
    #     import time
    #     try:
    #         if self.period !='' and self.period>0:
    #             nowyear = time.strftime('%Y',time.localtime(time.time()))
    #             return str(int(nowyear)-int(self.period))+'年级'
    #         else:
    #             return '<font color=red>未登记年级</font>'
    #     except:
    #             return '<font color=red>未登记年级</font>'
    # getclass.short_description = u'年级'
    # getclass.allow_tags = True
    
#     def save(self, * args, ** kwargs):
#         super(self.__class__, self).save(*args, ** kwargs)
# #        student=Student.objects.get(employee=self.id)
#         emp=Employee.objects.get(id=self.id)
#         student=Student()
#         student.employee=emp
#         student.save()
# #        try:
#         grade=Grade.objects.get(period=self.period,banji=self.banji)
#         grade.studentlist.add(student)  
#         grade.save()
#         print 'save  ok--------------'
#        except:
#            print 'save  flase--------------'
#            pass
    
class Comment(models.Model):
    commenttype=models.ForeignKey(CommentSort,verbose_name='评论类型')
    to_id=models.CharField('所属内容',max_length=20,null =True, blank=True,)
    times=models.DateTimeField('发布时间',auto_now=True,)
    nickname=models.CharField('昵称',max_length=20,null =True, blank=True,)
    content=models.TextField('内容',null =True, blank=True,)
    class Meta:
        verbose_name = '评论'
        verbose_name_plural = '评论'
        db_table = 'Comment' 
        #app_label = u"评论管理"  
    def __unicode__(self):
         return  self.nickname+u'评论:'+self.content
     
    def under(self):
        r = ''
        if self.commenttype.sort == ltyx.settings.SORTLIST[0]['sort']:
            r = Article.objects.get(id=ltyx.settings.SORTLIST[0]['id']).title
        if self.commenttype.sort ==ltyx.settings.SORTLIST[1]['sort']:
            r = Blog.objects.get(id=ltyx.settings.SORTLIST[1]['id']).title
        if self.commenttype.sort ==ltyx.settings.SORTLIST[2]['sort']:
            r = Grade.objects.get(id=ltyx.settings.SORTLIST[2]['id']).name + Grade.objects.get(id=ltyx.settings.SORTLIST[0]['id']).banji
        return r

'''
博客
photos：博文图片
'''   
class Blog(models.Model):
    TYPES_CHOICES = (
    ('Y', '原创'),
    ('Z', '转载'),
    )
    author = models.ForeignKey(Employee,verbose_name='作者')
    title = models.CharField('标题',max_length=20,)
    times = models.DateTimeField('发布时间',auto_now=True)
    content = models.TextField('内容',null =True, blank=True,)
    comment=models.ManyToManyField(Comment,verbose_name='评论',null =True, blank=True,)
    photos=models.ManyToManyField(Photos,verbose_name='博文图片',null =True, blank=True,)
    types=models.CharField('类型',max_length=10,choices=TYPES_CHOICES,null =True, blank=True)
    class Meta:
        verbose_name = '博客管理'
        verbose_name_plural = '博客管理'
        db_table = 'Blog' 
        #app_label = u"博客管理"  

    def __unicode__(self):
        return  self.title
    def getgrade(self):
        import time
        nowyear = time.strftime('%Y',time.localtime(time.time()))
        if int(nowyear)-int(self.author.period)>6:
            return '已经毕业！'
        if self.author.period !='' and self.author.period>0:
            print int(nowyear),
            return str(int(nowyear)-int(self.author.period))+'年级'
        else:
            return '<font color=red>未登记年级</font>'
#    getgrade.verbose_name='123'
    getgrade.short_description = u'年级'
    getgrade.allow_tags = True

    
'''照片'''
class Photos(models.Model):
    
    title = models.CharField('照片名称',max_length=20)
    images = models.ImageField(upload_to = UPLOAD_ROOT, blank = True,verbose_name='照片')
    class Meta:
        verbose_name = '照片'
        verbose_name_plural = '照片'
        db_table = 'Photos' 
        #app_label = u"相册管理"  
    def __unicode__(self):
        return  self.title  



'''相册分类'''
class PhotosSort(models.Model):
    title = models.CharField('相册名',max_length=20)
    PhotosSort = models.ManyToManyField(Photos,verbose_name='照片')
    class Meta:
        verbose_name = '相册'
        verbose_name_plural = '相册'
        db_table = 'PhotosSort' 
        #app_label = u"相册管理"  
    def __unicode__(self):
        return  self.title  

class NavSort(models.Model):
    title=models.CharField('标题',max_length=20)
    class Meta:
        verbose_name = '目录'
        verbose_name_plural = '目录'
        db_table = 'NavSort' 
        #app_label = u"目录管理"  
    def __unicode__(self):
        return  self.title    
    
class Nav(models.Model):
    navsort=models.ForeignKey(NavSort,verbose_name='导航',null =True, blank=True,)
    title=models.CharField('标题',max_length=20)
    class Meta:
        verbose_name = '子目录'
        verbose_name_plural = '子目录'
        db_table = 'Nav' 
        #app_label = u"目录管理"  
        
    def __unicode__(self):
        return  self.title    
    
    
'''
评论
commenttype 评论类型（博客评论 文章评论  班级评论）
'''
class CommentSort(models.Model):
    sort=models.CharField('评论类型',max_length=20)
    class Meta:
        verbose_name = '评论分类'
        verbose_name_plural = '评论分类'
        db_table = 'CommentSort' 
        #app_label = u"评论管理"  
    def __unicode__(self):
        return  self.sort  

class Comment(models.Model):
    commenttype=models.ForeignKey(CommentSort,verbose_name='评论类型')
    to_id=models.CharField('所属内容',max_length=20,null =True, blank=True,)
    times=models.DateTimeField('发布时间',auto_now=True,)
    nickname=models.CharField('昵称',max_length=20,null =True, blank=True,)
    content=models.TextField('内容',null =True, blank=True,)
    class Meta:
        verbose_name = '评论'
        verbose_name_plural = '评论'
        db_table = 'Comment' 
        #app_label = u"评论管理"  
    def __unicode__(self):
         return  self.nickname+u'评论:'+self.content
     
    def under(self):
        r = ''
        if self.commenttype.sort == ltyx.settings.SORTLIST[0]['sort']:
            r = Article.objects.get(id=ltyx.settings.SORTLIST[0]['id']).title
        if self.commenttype.sort ==ltyx.settings.SORTLIST[1]['sort']:
            r = Blog.objects.get(id=ltyx.settings.SORTLIST[1]['id']).title
        if self.commenttype.sort ==ltyx.settings.SORTLIST[2]['sort']:
            r = Grade.objects.get(id=ltyx.settings.SORTLIST[2]['id']).name + Grade.objects.get(id=ltyx.settings.SORTLIST[0]['id']).banji
        return r


#for  s in ss:
#    print s['id']
    
'''
文章
blog_type 文章所在分组
comment 评论
times 文章提交时间
'''   
    
#图片上传位置
UPLOAD_ROOT = './upload'
class Article(models.Model):
    blog_nav = models.ForeignKey(Nav,verbose_name='目录')
    author = models.ForeignKey(Employee,verbose_name='发布人')
    title = models.CharField('标题',max_length=20)
    images = models.ImageField(upload_to = UPLOAD_ROOT, null =True, blank=True,verbose_name='缩略图')
    times = models.DateTimeField('发布时间',auto_now=True)
    hits = models.IntegerField('点击量',max_length=1,null =True, blank=True,default=0)
    content = models.TextField('内容',null =True, blank=True,)
    comment=models.ManyToManyField(Comment,verbose_name='评论',null =True, blank=True,)
    class Meta:
        verbose_name = '文章管理'
        verbose_name_plural = '文章管理'
        #app_label = u"消息公告"  
        db_table = 'Article' 
    def __unicode__(self):
        return  self.title



'''照片'''
class Photos(models.Model):
    
    title = models.CharField('照片名称',max_length=20)
    images = models.ImageField(upload_to = UPLOAD_ROOT, blank = True,verbose_name='照片')
    class Meta:
        verbose_name = '照片'
        verbose_name_plural = '照片'
        db_table = 'Photos' 
        #app_label = u"相册管理"  
    def __unicode__(self):
        return  self.title  



'''相册分类'''
class PhotosSort(models.Model):
    title = models.CharField('相册名',max_length=20)
    PhotosSort = models.ManyToManyField(Photos,verbose_name='照片')
    class Meta:
        verbose_name = '相册'
        verbose_name_plural = '相册'
        db_table = 'PhotosSort' 
        #app_label = u"相册管理"  
    def __unicode__(self):
        return  self.title  

'''
博客
photos：博文图片
'''   
class Blog(models.Model):
    TYPES_CHOICES = (
    ('Y', '原创'),
    ('Z', '转载'),
    )
    author = models.ForeignKey(Employee,verbose_name='作者')
    title = models.CharField('标题',max_length=20,)
    times = models.DateTimeField('发布时间',auto_now=True)
    content = models.TextField('内容',null =True, blank=True,)
    comment=models.ManyToManyField(Comment,verbose_name='评论',null =True, blank=True,)
    photos=models.ManyToManyField(Photos,verbose_name='博文图片',null =True, blank=True,)
    types=models.CharField('类型',max_length=10,choices=TYPES_CHOICES,null =True, blank=True)
    class Meta:
        verbose_name = '博客管理'
        verbose_name_plural = '博客管理'
        db_table = 'Blog' 
        #app_label = u"博客管理"  

    def __unicode__(self):
        return  self.title
    def getgrade(self):
        import time
        nowyear = time.strftime('%Y',time.localtime(time.time()))
        if int(nowyear)-int(self.author.period)>6:
            return '已经毕业！'
        if self.author.period !='' and self.author.period>0:
            print int(nowyear),
            return str(int(nowyear)-int(self.author.period))+'年级'
        else:
            return '<font color=red>未登记年级</font>'
#    getgrade.verbose_name='123'
    getgrade.short_description = u'年级'
    getgrade.allow_tags = True

'''
学科表
'''    
class Subject(models.Model):
    subjecttype=models.ForeignKey(Employee,verbose_name="回复人")

    class Meta:
        verbose_name = '学科管理'
        verbose_name_plural = '学科管理'
        db_table = 'Subject' 
        #app_label = u"学科管理"  

    def __unicode__(self):
        return  self.subjecttype


'''
学生表
employee 学生是用户   blog学生博文
'''
class Student(models.Model):
    employee=models.ForeignKey(Employee,verbose_name='用户名')
    blog=models.ManyToManyField(Blog,verbose_name="BLOG",null =True, blank=True,)
    banji=models.ForeignKey(Banji,verbose_name='所在班级')
    isvalid=models.BooleanField('是否有效',default=False,)
    class Meta:
        verbose_name = '学生管理'
        verbose_name_plural = '学生管理'
        db_table = 'Student' 
        #app_label = u"学生管理"  
    def __unicode__(self):
        return  self.employee.user.username+'(%s)'%self.employee.nickname
    
    def getgrade(self):
        import time
        if self.employee.period !='' and self.employee.period>0:
            nowyear = time.strftime('%Y',time.localtime(time.time()))
            return str(int(nowyear)-int(self.employee.period))+'年级'
        else:
            return '<font color=red>未登记年级</font>'
#    getgrade.verbose_name='123'
    getgrade.short_description = u'年级'
    getgrade.allow_tags = True
        
    
'''
教师表
employee   教室是用户
ismaster     是否班主任
blog    发表的文章
subject    所教的学科
'''
class Teacher(models.Model):

    employee=models.ForeignKey(Employee,verbose_name='用户名')
    ismaster=models.BooleanField('是否班主任',default=False,)
    subject=models.CharField('学科',max_length=20)
    banji=models.ForeignKey(Banji,verbose_name='所在班级')
    class Meta:
        verbose_name = '教师管理'
        verbose_name_plural = '教师管理'
        db_table = 'Teacher' 
        #app_label = u"教师管理"  
    def __unicode__(self):
        master=''
        if self.ismaster == True:
            master=u',班主任'
        return  self.employee.user.username+'(%s %s)'%(self.employee.nickname,master) 
    
    def getgrade(self):
        import time
        if self.employee.period !='' and self.employee.period>0:
            nowyear = time.strftime('%Y',time.localtime(time.time()))
            return str(int(nowyear)-int(self.employee.period))+'年级'
        else:
            return '<font color=red>未登记年级</font>'
#    getgrade.verbose_name='123'
    getgrade.short_description = u'年级'
    getgrade.allow_tags = True


class Question(models.Model):
    user=models.ForeignKey(Employee,verbose_name="回复人")
    quizzer=models.CharField("提问者",max_length=20)
    question=models.TextField('问题',blank=True)
    reply=models.TextField("回复",blank=True,null =True,)
    times=models.DateTimeField('时间', auto_now=True,)
    hidden=models.BooleanField('是否隐藏',default=False,)

'''
问题
user 回复用户
quizzer 提问者或者游客
question 提问者所提出的问题
reply 回复用户所回复的内容
hidden 是否隐藏
'''
class Question(models.Model):
    user=models.ForeignKey(Employee,verbose_name="回复人")
    quizzer=models.CharField("提问者",max_length=20)
    question=models.TextField('问题',blank=True)
    reply=models.TextField("回复",blank=True,null =True,)
    times=models.DateTimeField('时间', auto_now=True,)
    hidden=models.BooleanField('是否隐藏',default=False,)
    class Meta:
        verbose_name = '问题管理'
        verbose_name_plural = '问题管理'
        db_table = 'Question' 
        #app_label = u"问题管理"  

    def __unicode__(self):
        return  self.question



    
NIANJI_CHOICES = (
('1', '一年级'),
('2', '二年级'),
('3', '三年级'),
('4', '四年级'),
('5', '五年级'),
('6', '六年级'),
)

'''作业表'''
class Teacher(models.Model):

    teacher=models.ForeignKey(Employee,verbose_name='用户名')
    times=models.BooleanField('是否班主任',default=False,)
    task=models.CharField('学科',max_length=20)
    banji=models.ForeignKey(Banji,verbose_name='所在班级')

    
'''班级表'''
class Grade(models.Model):
    banji=models.ForeignKey(Banji,verbose_name='所在班级')
    nickname = models.CharField('班级别名',max_length=20,null =True, blank=True,)
    task=models.TextField('作业',null=True,blank = True)
    times=models.DateTimeField('作业发布时间',null =True, blank=True,)
    photos=models.ManyToManyField(PhotosSort,verbose_name='班级相册',null =True, blank=True,)
    studentlist=models.ManyToManyField(Student,verbose_name=u'学生列表',null =True, blank=True,)
    teacherlist=models.ManyToManyField(Teacher,verbose_name=u'教师列表',null =True, blank=True,)
    comment=models.ManyToManyField(Comment,verbose_name='评论',null =True, blank=True,)
    class Meta:
        verbose_name = '班级管理'
        verbose_name_plural = '班级管理'
        db_table = 'Grade' 
        #app_label = u"教师管理"  
    def __unicode__(self):
        import time
        nowyear = time.strftime('%Y',time.localtime(time.time()))
        return  str(int(nowyear)-int(self.period))+u'年级 '+ self.banji+u'班'
    def get_absolute_url(self):
        return 'http://localhost/admin/ltyxapp/grade/?period=2012&banji__exact=1'

    def slist(self):
        list = []
        for s in studentlist:
            print s
        return list
            
        
    




    
    